package com.itheima.controller;


import com.itheima.common.R;

import com.itheima.pojo.dto.employeeDto.EmployeeAddDto;
import com.itheima.pojo.dto.employeeDto.EmployeeLoginDto;
import com.itheima.pojo.dto.employeeDto.EmployeeUpdateDto;
import com.itheima.pojo.vo.employeeVo.EmployeeLoginVo;
import com.itheima.pojo.vo.employeeVo.EmployeePageVo;
import com.itheima.service.EmployeeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tcotk
 */
@RestController
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;

    @PostMapping("/employee/login")
    @ResponseBody
    public R<Object> login(@RequestBody EmployeeLoginDto employeeLoginDto) {
        EmployeeLoginVo employeeLoginVo = employeeService.login(employeeLoginDto);

        if (employeeLoginVo != null) {
            return R.success(employeeLoginVo);
        }
        return R.error("登入失败");
    }

    @PostMapping("/employee/logout")
    @ResponseBody
    private R<String> loginOut() {

        return R.success("登出成功");
    }

    @PostMapping("/employee")
    @ResponseBody
    public R<String> add(@RequestBody EmployeeAddDto employeeAddDto) {
        if (employeeService.add(employeeAddDto)) {
            return R.success("添加成功");
        }
        return R.error("添加失败");

    }

    @PutMapping("/employee")
    @ResponseBody
    public R<String> update(@RequestBody EmployeeUpdateDto employeeUpdateDto) {
        boolean result = employeeService.update(employeeUpdateDto);
        if (result) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    @GetMapping("/employee/{id}")
    public R<Object> selectById(@PathVariable("id") String id) {

        return R.success(employeeService.selectById(id));
    }

    @GetMapping("/employee/page")
    public R<Map> selectByPage(int page, int pageSize, String name) {
        List<EmployeePageVo> employees = employeeService.selectByPage(page, pageSize, name);

        Map<String, Object> map = new HashMap<>();
        map.put("records", employees);
        map.put("total", (page - 1) * pageSize);
        map.put("size", pageSize);
        map.put("current", page);
        return R.success(map);
    }


}
